<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Model Tool</title>
    <style type="text/css">
    .top_container{
    	text-align:right;
    	margin-right:350px;
    }
    </style>
    <!-- Combo-handled YUI CSS files: -->
	<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.7.0/build/datatable/assets/skins/sam/datatable.css&2.7.0/build/logger/assets/skins/sam/logger.css">
	<!-- Combo-handled YUI JS files: -->
	<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo/yahoo.js&2.7.0/build/event/event.js&2.7.0/build/connection/connection.js&2.7.0/build/datasource/datasource.js&2.7.0/build/dom/dom.js&2.7.0/build/dragdrop/dragdrop.js&2.7.0/build/element/element.js&2.7.0/build/datatable/datatable.js&2.7.0/build/json/json.js&2.7.0/build/logger/logger.js"></script>  
</head>
<body class=" yui-skin-sam">
    <div id="div_login" class="top_container">
        <div id="div_login_host">
            <span>Host:</span> 
            <span>
                <input type="text" id="input_host" value="127.0.0.1" /></span>
        </div>
        <div id="div_login_port">
            <span>Port:</span> 
            <span>
                <input type="text" id="input_port" value="3306" /></span>
        </div>
        <div id="div_login_user">
            <span>UserName:</span> 
            <span>
                <input type="text" id="input_username" value="root" /></span>
        </div>
        <div id="div_login_pwd">
            <span>Password:</span> 
            <span>
                <input type="password" id="input_password" value="windfir" /></span>
        </div>
        <div id="div_login_dbName">
            <span>Database:</span> 
            <span>
                <input type="text" id="input_dbName" value="simplespace" /></span>
        </div>
        <div id="div_login_submit">
            <button onclick="showTables();" >Show tables</button>
        </div>
        <div id="div_showFieldsBtn">
            <button id="btnDownload" onclick="downloadModelMap()" disabled="disabled">Download model map</button>
            <button id="btnDownloadAll" onclick="downloadAllModelMap()" disabled="disabled">Download all model map</button>
        </div>
        <div id="div_tableList"></div>
    	<div id="divLog"></div>
    </div>
	
	<form name="downloadForm" id="downloadForm" method="post" target="_self" style="display:none;">
		<input type="hidden" name="data" id="data" />
	</form>
    <script type="text/javascript">
    YAHOO.util.Event.throwErrors = true;
    //var logger = new YAHOO.widget.LogReader("divLog");    
    var oDataTable = null;
    var sTableListKey = "";
    var models = {database:null,table:null};
    
    function showTables()
    {
        var host = document.getElementById("input_host").value;
        var port = document.getElementById("input_port").value;
        var username = document.getElementById("input_username").value;
        var password = document.getElementById("input_password").value;
        var database = document.getElementById("input_dbName").value;
        var dataObj = {Host 		: host,
                	   Port			: port,
                	   User 		: username,
                	   Password 	: password,
                	   DatabaseName : database
                	   };
 	    models.database = {model:"DatabaseModel",value:dataObj};
        var data = YAHOO.lang.JSON.stringify(dataObj);
        var postData = "data=" + data;
        var successHandler = function handleLoginSuccess(p)
        {
            YAHOO.log("Receive from server:"+p.responseText);
            sTableListKey = "TableName";
            var myColumnDefs = [
                    {key:sTableListKey,field:sTableListKey,label:"表名", sortable:true, resizeable:true}
                ];

            var myDataSource = new YAHOO.util.DataSource(YAHOO.lang.JSON.parse(p.responseText));
            myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;

            oDataTable = new YAHOO.widget.DataTable("div_tableList",
                    myColumnDefs, myDataSource);
            oDataTable.subscribe("rowMouseoverEvent",oDataTable.onEventHighlightRow);        
            oDataTable.subscribe("rowMouseoutEvent",oDataTable.onEventUnhighlightRow);
            oDataTable.subscribe("rowClickEvent",oDataTable.onEventSelectRow);

            document.getElementById("btnDownload").removeAttribute("disabled");
            document.getElementById("btnDownloadAll").removeAttribute("disabled");
        };
        var errorHandler = function handleLoginFailure(p)
        {
            alert(p.responseText);
        }
        var callback = 
        {
            success : successHandler,
            failure : errorHandler,
            argument : []
        }
        YAHOO.log("Begin request:index.php?a=showTables");
        YAHOO.log(postData);
        YAHOO.util.Connect.asyncRequest("POST","index.php?a=showTables",callback,postData);
                
    }
    
    
    function downloadModelMap()
    {
        var aRows = oDataTable.getSelectedRows();
        if(!aRows || aRows.length <= 0)
        {
            alert("Please select a table.");
			return;
        }
        var oRs = oDataTable.getRecordSet();
        var sTableName = oRs.getRecord(aRows[0]).getData().TableName;
        
        models.table = {model : "TableModel",value : {TableName : sTableName}};

		var newForm = document.getElementById("downloadForm");
		newForm.setAttribute("action","index.php?a=createModelMap");
		newForm.setAttribute("target","_self");
		newForm.setAttribute("method","post");

		var newField = document.getElementById("data");
		newField.setAttribute('name','data');
		newField.setAttribute('value',YAHOO.lang.JSON.stringify(models));

		newForm.submit();
		
    }

    function downloadAllModelMap()
    {
    	var newForm = document.getElementById("downloadForm");
		newForm.setAttribute("action","index.php?a=createAllModelMap");
		newForm.setAttribute("target","_self");
		newForm.setAttribute("method","post");

		var newField = document.getElementById("data");
		newField.setAttribute('name','data');
		newField.setAttribute('value',YAHOO.lang.JSON.stringify(models.database.value));

		newForm.submit();
    }
    </script>
</body>
</html>
